package com.zj.fx.service;

import com.zj.fx.common.base.BaseService;
import com.zj.fx.common.quartz.QuartzSchedulerUtil;
import com.zj.fx.common.quartz.ScheduleConstants;
import com.zj.fx.common.support.ConvertUtil;
import com.zj.fx.mapper.auto.SysQuartzJobMapper;
import com.zj.fx.model.auto.SysQuartzJob;
import com.zj.fx.model.auto.SysQuartzJobExample;
import com.zj.fx.model.custom.Tablepar;
import com.zj.fx.util.SnowflakeIdWorker;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 定时任务调度表 SysQuartzJobService
 *
 * @author fuce_自动生成
 * @Title: SysQuartzJobService.java
 * @Package com.zj.fx.service
 * @email 115889198@qq.com
 * @date 2019-09-13 00:03:35
 **/
@Service
@RequiredArgsConstructor
public class SysQuartzJobService implements BaseService<SysQuartzJob, SysQuartzJobExample> {
    private final SysQuartzJobMapper sysQuartzJobMapper;
    private final QuartzSchedulerUtil scheduler;

    /**
     * 分页查询
     *
     * @return
     */
    public PageInfo<SysQuartzJob> list(Tablepar tablepar, String name) {
        SysQuartzJobExample testExample = new SysQuartzJobExample();
        testExample.setOrderByClause("id ASC");
        if (name != null && !"".equals(name)) {
            testExample.createCriteria().andJobNameLike("%" + name + "%");
        }

        PageHelper.startPage(tablepar.getPage(), tablepar.getLimit());
        List<SysQuartzJob> list = sysQuartzJobMapper.selectByExample(testExample);
        PageInfo<SysQuartzJob> pageInfo = new PageInfo<SysQuartzJob>(list);
        return pageInfo;
    }

    @Override
    public int deleteByPrimaryKey(String ids) {
        List<String> lista = ConvertUtil.toListStrArray(ids);
        SysQuartzJobExample example = new SysQuartzJobExample();
        example.createCriteria().andIdIn(lista);
        return sysQuartzJobMapper.deleteByExample(example);
    }


    @Override
    public SysQuartzJob selectByPrimaryKey(String id) {

        return sysQuartzJobMapper.selectByPrimaryKey(id);
    }


    @Override
    public int updateByPrimaryKeySelective(SysQuartzJob record) {
        int i = sysQuartzJobMapper.updateByPrimaryKeySelective(record);
        if (i > 0) {
            //修改定时器
            scheduler.modifyJob(record);
        }
        return i;
    }

    /**
     * 添加
     */
    @Override
    public int insertSelective(SysQuartzJob record) {
        //添加雪花主键id
        record.setId(SnowflakeIdWorker.getUUID());
        return sysQuartzJobMapper.insertSelective(record);
    }


    @Override
    public int updateByExampleSelective(SysQuartzJob record, SysQuartzJobExample example) {

        return sysQuartzJobMapper.updateByExampleSelective(record, example);
    }


    @Override
    public int updateByExample(SysQuartzJob record, SysQuartzJobExample example) {

        return sysQuartzJobMapper.updateByExample(record, example);
    }

    @Override
    public List<SysQuartzJob> selectByExample(SysQuartzJobExample example) {

        return sysQuartzJobMapper.selectByExample(example);
    }


    @Override
    public long countByExample(SysQuartzJobExample example) {

        return sysQuartzJobMapper.countByExample(example);
    }


    @Override
    public int deleteByExample(SysQuartzJobExample example) {

        return sysQuartzJobMapper.deleteByExample(example);
    }

    /**
     * 检查name
     *
     * @param sysQuartzJob
     * @return
     */
    public int checkNameUnique(SysQuartzJob sysQuartzJob) {
        SysQuartzJobExample example = new SysQuartzJobExample();
        example.createCriteria().andJobNameEqualTo(sysQuartzJob.getJobName());
        List<SysQuartzJob> list = sysQuartzJobMapper.selectByExample(example);
        return list.size();
    }


    /**
     * 恢复任务
     *
     * @param job 调度信息
     */
    @Transactional
    public int resumeJob(SysQuartzJob job) throws SchedulerException {
        job.setStatus(ScheduleConstants.Status.NORMAL.getValue());
        int rows = sysQuartzJobMapper.updateByPrimaryKeySelective(job);
        if (rows > 0) {
            scheduler.resumeJob(job);
        }
        return rows;
    }


    /**
     * 暂停任务
     *
     * @param job 调度信息
     */
    @Transactional
    public int pauseJob(SysQuartzJob job) throws SchedulerException {
        job.setStatus(ScheduleConstants.Status.PAUSE.getValue());
        //job.setUpdateBy(ShiroUtils.getLoginName());
        int rows = sysQuartzJobMapper.updateByPrimaryKeySelective(job);
        if (rows > 0) {
            scheduler.pauseJob(job);
        }
        return rows;
    }

    /**
     * 任务调度状态修改
     *
     * @param job 调度信息
     */
    @Transactional
    public int changeStatus(SysQuartzJob job) throws SchedulerException {
        int rows = 0;
        Integer status = job.getStatus();
        if (ScheduleConstants.Status.NORMAL.getValue().equals(status)) {
            rows = resumeJob(job);
        } else if (ScheduleConstants.Status.PAUSE.getValue().equals(status)) {
            rows = pauseJob(job);
        }
        return rows;
    }


    /**
     * 立即运行任务
     *
     * @param job 调度信息
     */
    @Transactional
    public void run(SysQuartzJob job) throws SchedulerException {

        scheduler.run(job);

    }


}
